package com.qianyu.project.web.controller.gs;

import com.qianyu.project.core.context.user.FlowUserManager;
import com.qianyu.project.entity.SystemUser;
import com.qianyu.project.entity.TenderCompany;
import com.qianyu.project.service.ISystemUserService;
import com.qianyu.project.service.ITenderCompanyService;
import com.qianyu.project.utils.Result;
import com.qianyu.project.utils.ResultTool;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author gs
 * @date create in 2021/2/2 19:03
 */
@RestController
@RequestMapping("/tenderCompanys")
public class TenderCompanyController {

    @Autowired
    private ITenderCompanyService tenderCompanyService;
    @Autowired
    private ISystemUserService systemUserService;
    @Autowired
    private FlowUserManager flowUserManager;

    @PostMapping
    public Result register(@RequestBody TenderCompany tenderCompany) {
        tenderCompanyService.register(tenderCompany);
        return ResultTool.success(tenderCompany);
    }

    @PutMapping
    public Result update(@RequestBody TenderCompany tenderCompany) {
        tenderCompanyService.updateById(tenderCompany);
        return ResultTool.success();
    }

    @GetMapping("/exist")
    public Result checkExist() {
        SystemUser systemUser = systemUserService.getById(flowUserManager.getFlowUser().getId());
        if (systemUser.getCompanyId() == null) {
            return ResultTool.success("register");
        } else {
            return ResultTool.success("update").put(systemUser.getCompanyId());
        }
    }

    @GetMapping("{id}")
    public Result getById(@PathVariable String id) {
        return ResultTool.success(tenderCompanyService.getById(id));
    }
}
